package com.tcxhb.job.admin.model.request.biz;

import com.tcxhb.job.common.constants.JobTypeConstants;
import com.tcxhb.job.common.constants.enums.ScheduleTypeEnum;
import com.tcxhb.job.common.model.job.JobParams;
import com.tcxhb.job.common.model.job.RunStrategy;
import com.tcxhb.job.common.model.job.ScheduleConf;
import com.tcxhb.job.common.utils.ParamUtils;
import com.tcxhb.job.core.utils.SchedulerTimeUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * author:auto.generator
 * time: 2023-11-02
 */
@Data
public class JobInfoAddReq implements Serializable {
    @ApiModelProperty("")
    private Integer id;

    @ApiModelProperty("所属应用ID")
    private String appName;

    @ApiModelProperty("任务名称")
    private String jobName;

    @ApiModelProperty("调度类型：0,手动触发，1定时触发")
    private Integer scheduleType;

    @ApiModelProperty("调度配置，值含义取决于调度类型")
    private ScheduleConf scheduleConf;

    @ApiModelProperty("执行器任务handler，参数")
    private JobParams jobParams;

    @ApiModelProperty("运行参数配置: 重试次数，超时时间")
    private RunStrategy runStrategy;

    @ApiModelProperty("调度状态：0-停止，1-运行")
    private Integer status = 0;

    public void check() {
        ParamUtils.notEmpty(jobName, "任务名称不能为空");
        ParamUtils.notEmpty(appName, "应用不能为空");
        ParamUtils.notNull(scheduleType, "调度类型不能为空");
        ParamUtils.notNull(jobParams, "任务类型不能为空");
        //参数校验
        if (JobTypeConstants.JAVA_BEAN.equals(jobParams.getType())) {
            ParamUtils.notEmpty(jobParams.getHandler(), "beanName不能为空");
        }
        //调度类型
        if (ScheduleTypeEnum.Cron.eq(scheduleType)) {
            ScheduleConf conf = this.getScheduleConf();
            ParamUtils.notNull(conf, "cron表达式不能为空");
            ParamUtils.notNull(conf.getCron(), "cron表达式不能为空");
            Boolean check = SchedulerTimeUtils.isValidCronExpression(conf.getCron());
            ParamUtils.isTrue(check, "cron表达式错误");
            conf.setType(scheduleType);
        }
        //运行参数设置
        ParamUtils.notNull(runStrategy, "运行参数略不能为空");
        ParamUtils.notNull(runStrategy.getDistStrategy(), "分发策略不能为空");
    }
}
